// service/pendingOrder/pendingOrder.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    finish: false,
    loading: false,
    page: 1,

    couponShow: false,
    show: false,
    order_id: '', // 订单ID
    skill_id: '', // 服务人员ID
    name: '请选择',// 服务人员name
    columns:[],
  },


  handleOrder(){
    if(this.data.name === '请选择'){
      util.toast('请选择服务者');
      return;
    }
    
    http.post('order/updateskill',{
      order_id: this.data.order_id,
      skill_id: this.data.skill_id,
    }, true).then(res => {
      if(res.code !== 1){
        util.toast(res.msg)
        return;
      }
      
      this.reload();
      util.toast('分配成功');
      this.onClose();
    })
  },
  onConfirm(event) {
    const { value, index } = event.detail;
    this.setData({
      skill_id: value.id, // 服务人员ID
      name: value.name,// 服务人员name
    })
    this.onCancel()
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  onSelectPeople(){
    this.getPeopleList();
    this.setData({
      show: true
    })
  },
  openPopup(e){
    const item = e.currentTarget.dataset.item;
    this.setData({
      order_id: item.id,
      couponShow: true
    })
  },
  onClose(){
    this.setData({
      couponShow: false
    })
  },
  getPeopleList(){
    http.post('order/getSkill',{
      order_id:  this.data.order_id,
    }, true).then(res => {
      this.setData({
        columns: res.data.skillList,
      })
    })
  },


  reload() {
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more() {
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList() {
    if(!wx.getStorageSync('token')) {
      util.skip('/service/login/login')
      return
    }

    if (this.data.finish) {
      return
    }
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
      // status: 1,
      not_allocation: 1,
    }
    http.get('order/userOrderPdList', data).then(res => {
      if (res.data.length === 0) {
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data)
      this.setData({
        list: arr
      })

    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.more();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})